<?php
/**
 * 离职管理
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Hr_OutInterface');

class Bll_Flow_OperateLog extends BLL {

    private static $instance;

    /**
     * Returns instance.
     */
    public static function get_instance() {
        if (!self::$instance) {
            self::$instance = new Bll_Flow_OperateLog();
        }
        return self::$instance;
    }
    /**
     * insert
     * @param array $data
     */
    public function save_log($data){
        $return = false;
        if(empty($data['flow_id']) || empty($data['flow_opt'])){
            return $return;
        }
        $re = $this->get_log(intval($data['flow_id']), intval($data['flow_opt']));
        if(!$re){
            return $this->get_log_dao()->insert($data);
        }else{
            //如果是更新，则不能更新当前操作步骤
            unset($data['flow_stage']);
            //如果是更新，则不更新当前据色
            unset($data['role_type']);
            return $this->get_log_dao()->update_by_id($re['log_id'],$data);
        }
    }
    /**
     * 查询log信息
     * @param unknown_type $flow_id
     * @param unknown_type $flow_opt
     */
    public function get_log($flow_id, $flow_opt){
        return $this->get_log_dao()->find_row(array('flow_id'=>$flow_id,'flow_opt'=>$flow_opt));
    }
    /**
     * 检测一个审批人是否在流程中审批过
     */
    public function find_opt_log($flow_id,$user_id){
        return $this->get_log_dao()->find_count(array('flow_id'=>$flow_id,'opt_user_id'=>$user_id));
    }
    /**
     * 根据flow_id和role_type查询log信息
     * @param unknown_type $flow_id
     * @param unknown_type $role_type
     * @param unknown_type $flow_opt
     */
    public function get_log_by_id_role($flow_id, $role_type , $flow_opt){
        $condition = array('flow_id'=>$flow_id,'role_type'=>$role_type);
        if($flow_opt){
            $condition['flow_opt']= $flow_opt;
        }
        return $this->get_log_dao()->find_row($condition);
    }
    /**
     * 取消办事流程
     * @param int $flow_id
     */
    public function cancel_flow($flow_id){
        return $this->get_log_dao()->update(array('is_deleted'=>3),array('flow_id'=>$flow_id));
    }
    /**
     *根据flow_ids查询log
     */
    public function get_log_by_flow_ids($flow_ids){
        if(empty($flow_ids)){
            return array();
        }
        $_where["flow_id IN(".implode(',',$flow_ids).") AND 1=?"]=1;
        $datalist = $this->get_log_dao()->find($_where,'',0);
        return $datalist;
    }
    /**
     * 根据flow_id查询log
     * @param int $flow_id
     */
    public function get_flow_log($flow_id,$flow_stage=0){
        if(!$flow_id){
            return array();
        }
        $data = array('flow_id'=>intval($flow_id));
        if($flow_stage){
            $data['flow_stage'] = intval($flow_stage);
        }
        $datalist = $this->get_log_dao()->find($data,'',0);
        return $datalist;
    }
    /**
     * 获取操作中所有人的信息
     * @param unknown_type $flow_id
     */
    public function get_flow_user_log($flow_id){
        $tmp_loglist = $this->get_flow_log($flow_id);
        $uids = array();
        $loglist = array();
        foreach($tmp_loglist as $val){
            $uids[$val['flow_opt']] = $val['opt_user_id'];
            $loglist[$val['flow_opt']] = $val;
        }
        $uids = array_values($uids);
        $uids = array_unique($uids);
        $bll_hroa = new Bll_Hr_OutInterface();
        $user_data = $bll_hroa->get_user_by_ids($uids);
        foreach($loglist as $k=> $val){
            $loglist[$k]['opt_user_name'] = $user_data[$val['opt_user_id']]['name'];
        }
        return $loglist;
    }
    /**
     * 获取操作中所有人的信息
     * @param unknown_type $flow_id
     */
    public function get_flow_user_logs($flow_ids){
        $tmp_loglist = $this->get_flow_log($flow_id);
        $uids = array();
        $loglist = array();
        foreach($tmp_loglist as $val){
            $uids[$val['flow_opt']] = $val['opt_user_id'];
            $loglist[$val['flow_opt']] = $val;
        }
        $uids = array_values($uids);
        $uids = array_unique($uids);
        $bll_hroa = new Bll_Hr_OutInterface();
        $user_data = $bll_hroa->get_user_by_ids($uids);
        foreach($loglist as $k=> $val){
            $loglist[$k]['opt_user_name'] = $user_data[$val['opt_user_id']]['name'];
        }
        return $loglist;
    }
    /**
     * 已受理列表
     * @param int $uid,办理人
     * @param array $params
     * @param int $limit
     * @param int $offset
     * @return multitype:unknown
     */
    public function get_flow_done_list($params, $limit, $offset){
        $data = array(
                'count'=>0,
                'datalist'=>array()
        );
        $_where = $this->_bulid_where($params);

        $count = $this->get_log_dao()->find_count($_where,'flow_id');
        if(!$count){
            return $data;
        }
        $field = array('DISTINCT flow_id,effect_date,user_id,user_name,work_city_id,flow_type');
        $datalist = $this->get_log_dao()->find_short($_where,' effect_date DESC ,flow_id DESC',$limit,$offset,$field);
        return array('count'=>$count,'datalist'=>$datalist);
    }
    /**
     * 构造搜索条件
     * @param unknown_type $params
     * @return multitype:string
     */
    private function _bulid_where($params){
        $_where['opt_user_id'] = $params['user_id'];
        if(isset($params['city_id'])){
            $_where["work_city_id"] = intval($params['city_id']);
        }
        if(isset($params['flow_type'])){
            if($params['flow_type'] == Const_Flow::FLOW_TYPE_TRANSFER){
                $_where["flow_type IN(".Const_Flow::FLOW_TYPE_TRANSFER.",".Const_Flow::FLOW_TYPE_MONEY.") AND 1=?"] = 1;
            }else{
                $_where["flow_type"] = intval($params['flow_type']);
            }
        }
        if(isset($params['user_name'])) {
            $_where["user_name LIKE '{$params['user_name']}%' AND 1=?"] = 1;
        }
        if(isset($params['start']) && isset($params['end'])){
            $_where["effect_date >= ".$params['start']." AND effect_date<= ".$params['end']." AND 1=?"] = 1;
        }
        return $_where;
    }
    /**
     * 修改时批量修改生效日期
     * @param int $flow_id
     * @param int $date
     */
    public function update_plan_date($flow_id,$date){
        return $this->get_log_dao()->update(array('effect_date'=>$date),array('flow_id'=>$flow_id));
    }
    /**
     * 修改时批量修改生效日期,用户名，工作城市
     * @param int $flow_id
     * @param int $date
     */
    public function update_log_info($flow_id,$name,$cityid,$date){
        return $this->get_log_dao()->update(array('user_name'=>$name,'work_city_id'=>$cityid,'effect_date'=>$date),array('flow_id'=>$flow_id));
    }
    /**
     * 修改log工作城市
     * @param int $flow_id
     */
    public function update_city_id($flow_id,$cityid){
        return $this->get_log_dao()->update(array('work_city_id'=>$cityid),array('flow_id'=>$flow_id));
    }
    private function get_log_dao(){
        $dao = $this->get_dao('Dao_Flow_OperateLog');
        return $dao;
    }
}